package com.bhp.bhphrserver.security;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bhp.bhphrserver.entity.Hr;
import com.bhp.bhphrserver.service.HrService;
import com.bhp.bhphrserver.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @ClassName UserService
 * @Description TODO
 * @Author 包海鹏
 * @Date 2020/4/30 10:47
 * @Version 1.0
 **/
@Service
public class UserService implements UserDetailsService {

    @Autowired
    private HrService hrService;
    @Autowired
    private RoleService roleService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        QueryWrapper<Hr> qw = new QueryWrapper<>();
        qw.eq("username", username);
        Hr hr = hrService.getOne(qw);
        if (hr == null) {
            throw new UsernameNotFoundException("用户名不存在！");
        }
        User user = new User();
        BeanUtils.copyProperties(hr, user);
        user.setRoles(roleService.getRolesByHrId(hr.getId()));
        return user;
    }
}
